import axios from 'axios'
import store from '../store/index.js'
// import baseURL from './baseURL'
import { Message ,Loading } from 'element-ui'
const options = {
    text: '载入中...',
    background: 'rgba(255,255,255,0.6)',
    spinner: 'el-icon-loading',
    lock: true,
    target: '.right-container' // target指定需要覆盖的 DOM 节点，会获取到问的那个中第一个类名为right-container的元素
}
let loadingInstance;

const http = {}


var instance = axios.create({
    timeout: 5000,
    // baseURL,
})

// 添加请求拦截器
instance.interceptors.request.use(
    function(config) {
        // console.log("111")   
        loadingInstance = Loading.service(options); // 以服务的方式调用的 Loading
        
        // console.log(store.state.UserToken)
        // 请求头添加token
        if (store.state.UserToken) {
            config.headers.token = store.state.UserToken
        }
        return config
    },
    function(error) {
        loadingInstance.close();
        console.log("请求拦截 error")
        return Promise.reject(error)
    }
)

// 响应拦截器即异常处理
instance.interceptors.response.use(
    response => {
        loadingInstance.close();
        return response.data
    },
    err => {
        loadingInstance.close();
        if (err && err.response) {
            switch (err.response.code) {
            case 400:
                err.message = '请求出错'
                break
            case 401:
                Message.warning({
                    message: '授权失败，请重新登录'
                })
                setTimeout(() => {
                    window.location.reload()
                }, 1000)

                return
            case 403:
                err.message = '拒绝访问'
                break
            case 404:
                err.message = '请求错误,未找到该资源'
                break
            case 500:
                err.message = '服务器端出错'
                break
            }
        } else {
            err.message = '连接服务器失败'
        }
        console.log(1);
        Message.error({          
            message: err.message
        })
        return Promise.reject(err.response)
    }
)

http.get = function(url, options) {
    return new Promise((resolve, reject) => {
        instance
            .get(url, options)
            .then(response => {
                if (response.code === 200) {
                    resolve(response.data)
                }else if(response.code==401){
                    localStorage.setItem("token","");
                } else {
                    console.log("http get error");
                    Message.error({
                        message: response.msg
                    })
                    reject(response.msg)
                }
            })
            .catch(e => {
                console.log(e)
            })
    })
}

http.post = function(url, data, options) {
    return new Promise((resolve, reject) => {
        instance
            .post(url, data, options)
            .then(response => {
                console.log("http post then")
                if (response.code === 200) {
                    resolve(response.data)
                } else if(response.code==401){
                    // store.dispatch("LOGIN_OUT");
                } else {
                    console.log("http post error");
                    Message.error({
                        message: response.msg
                    })
                    reject(response.msg)
                }
            })
            .catch(e => {
                console.log("http post chatch")
                // console.log(e)
            })
    })
}

http.delete=function (url,options){
    return new Promise(((resolve, reject) => {
        instance.delete(url,options)
            .then((response)=>{
                console.log("delete resolve")
                if (response.code === 200) {
                    resolve(response.data)
                } else {
                    Message.error({
                        message: response.msg
                    })
                    reject(response.msg)
                }
            })
            .catch(e=>{
                console.log("delete catch")
            })
    }))
}
export default http
